#include "HandlerRegister.h"

void HandlerRegister::Register(const std::string& command, IHandler* prototype)
{
	_prototypes[command] = prototype;
}

HandlerRegister& HandlerRegister::Instance()
{
	static HandlerRegister s_instance;
	return s_instance;
}

std::vector<std::string> HandlerRegister::GetCommandList() const
{
    auto list = std::vector<std::string>(_prototypes.size());
    for (auto entry : _prototypes)
    {
        list.push_back(entry.first);
    }
    return list;
}

HandlerProvider::HandlerProvider(const std::string& command, const IHandler& handler)
{
	HandlerRegister::Instance().Register(command, handler.Clone());
}

std::shared_ptr<IHandler> HandlerRegister::GetHandler(const std::string& command) const
{
    auto iterator = _prototypes.find(command);
    if (iterator == _prototypes.end()) 
	{
        return nullptr;
    }
    auto clonedHandler = iterator->second->Clone();
    return std::shared_ptr<IHandler>(clonedHandler);
}